package com.codeyang.mocktest2.mockit;

import com.codeyang.mocktest2.controller.MvcController;
import com.codeyang.mocktest2.entity.TUser;
import com.codeyang.mocktest2.service.TUserService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述: describe
 *
 * @author shengyang5
 * @version 2021/7/23 13:49
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class InjectMocksTest2 {

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

    /**
     * 如果@InjectMocks 是一个接口 需要 new 指定类
     * 不能是接口
     * <p>
     * org.apache.ibatis.binding.MapperProxy.PlainMethodInvoker#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], org.apache.ibatis.session.SqlSession)
     * PropertyAndSetterInject
     * mapperMethod.execute(sqlSession, args);
     * 如果使用@Spy 代理类的bean依赖关系并没有被处理
     */

    // 不注入也可以
    // @Autowired
    @InjectMocks
    private MvcController mvcController;

    @SpyBean
    private TUserService tUserService;

    @Test
    public void getAll() {
        List<TUser> tUsers = new ArrayList<>();

        // tUsers.add(new TUser(999, "测试数据", 99, "古巴"));
        // when(tUserService.queryAll()).thenReturn(tUsers);

        System.out.println(mvcController.getAll());
        if (tUsers.equals(mvcController.getAll())) {
            System.out.println(true);
        }
    }

}
